/*
  Copyright (C) 2011, Kyungjoo Kim
  
  This file is part of LINAL (LINear ALgebra)
  
  All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  Also add information on how to contact you by electronic and paper mail.
  
  
  Kyungjoo Kim
  iamkyungjoo@gmail.com
*/

#include "linal/common.hxx"
#include "linal/util.hxx"

namespace linal {

  static int s_argc;
  static char **s_argv;

  static bool s_verbose;
  static int s_nthreads;

  // --------------------------------------------------------------
  // ** Computing model setup
  void initialize(int argc, char **argv) {

    FLA_Init();
    // hold command line inputs
    s_argc = argc;
    s_argv = argv;

    get_option_bool("-verbose", "enable verbose ouput", 
                    __FILE__, false, s_verbose);

    get_option_int( "-nthreads", "# of threads",
                    __FILE__,1, s_nthreads);
    omp_set_num_threads( s_nthreads );
  }
  void finalize() {
    s_argc = 0; 
    s_argv = NULL;

    s_verbose = false;
    FLA_Finalize();
  }

  void verbose_begin() { s_verbose = true; }
  void verbose_end()   { s_verbose = false; }

  bool get_verbose()   { return s_verbose; }
  int  get_num_threads() { return s_nthreads; }

  void get_option_bool(const char *opt, const char *text, const char *man,
                       bool def, bool &val, int mode) {
    switch (mode) {
    case LINAL_OPTION_MODE_DRY:
      {
        std::cout << "[LINAL]"
                  << "bool option " << opt
                  << " has default " << def
                  << ", " << text
                  << " " << man << std::endl;
        break;
      }
    case LINAL_OPTION_MODE_RUN:
      {
        val = def;
        for (int i=1;i<s_argc;++i)
          if (std::string(s_argv[i])==opt) 
            val = true;

        if (get_verbose())
          std::cout << "[LINAL]"
                    << "bool option " << opt
                    << " has been set to " << val
                    << ", " << text
                    << " " << man << std::endl;
        break;
      }
    }
  }
  void get_option_int (const char *opt, const char *text, const char *man,
                       int def, int &val, int mode) {
    switch (mode) {
    case LINAL_OPTION_MODE_DRY:
      {
        std::cout << "[LINAL]"
                  << "int option " << opt
                  << " has default " << def
                  << ", " << text
                  << " " << man << std::endl;
        break;
      } 
    case LINAL_OPTION_MODE_RUN:
      {
        val = def;
        for (int i=1;i<s_argc;++i)
          if (std::string(s_argv[i])==opt)
            val = atoi(s_argv[++i]);
        
        if (get_verbose())
          std::cout << "[LINAL]"
                    << "int option " << opt
                    << " has been set to " << val
                    << ", " << text
                    << " " << man << std::endl;
        break;
      }
    }
  }
  void get_option_double(const char *opt,const char *text,const char *man,
                         double def, double &val, int mode) {
    switch (mode) {
    case LINAL_OPTION_MODE_DRY:
      {
        std::cout << "[LINAL]"
                  << "double option " << opt
                  << " has default " << def
                  << ", " << text
                  << " " << man << std::endl;
        break;
      }
    case LINAL_OPTION_MODE_RUN:
      {
        val = def;
        for (int i=1;i<s_argc;++i)
          if (std::string(s_argv[i])==opt)
            val = atof(s_argv[++i]);
        
        if (get_verbose())
          std::cout << "[LINAL]"
                    << "double option " << opt
                    << " has been set to " << val
                    << ", " << text
                    << " " << man << std::endl;
        break;
      }
    }
  }
}
